package org.karai.frame.core.okhttpUtils;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;

import org.json.JSONObject;
import org.karai.frame.core.base.IBaseModel;
import org.karai.frame.core.base.IBasePresenter;
import org.karai.frame.core.base.IContanst;

import java.io.IOException;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;



public class OkhttpUtils {


    public static OkHttpClient mOkHttpClient = null;
    private Request.Builder requestBuilder = new Request.Builder();

    public static OkhttpUtils okhttpUtils = null ;

    /**
     * base url
     */
    public static final String BASE_URL = "";

    public static OkhttpUtils builder() {

        if (mOkHttpClient == null) {
            mOkHttpClient = new OkHttpClient().newBuilder()
                    .callTimeout(200, TimeUnit.MILLISECONDS)
                    .readTimeout(200, TimeUnit.MILLISECONDS)
                    .build();
        }
        okhttpUtils = new OkhttpUtils();
        return okhttpUtils;
    }


    /**
     * 普通Get请求
     */
    public  void HttpGet(String url, String code ,Activity activity , IBasePresenter iBasePresenter ) {
        new Thread(()->{
            Response response = null ;
            Request request = requestBuilder
                    .url(BASE_URL + url)
                    .get()
                    .build();
            try {
                response = mOkHttpClient.newCall(request).execute();
                String string = response.body().string();
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            iBasePresenter.success(code ,string);
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }).start();

    }

    /**
     * 普通Post请求
     */
    public  void HttpPost(String url, String code , Map<String,Object> paramData ,  Activity activity , IBasePresenter iBasePresenter ) {
        new Thread(()->{
            Response response = null ;

            RequestBody requestBody = RequestBody.create(MediaType.parse("application"), new JSONObject(paramData).toString());
            Request request = requestBuilder
                    .url(BASE_URL + url)
                    .post(requestBody)
                    .build();
            try {
                response = mOkHttpClient.newCall(request).execute();
                String string = response.body().string();
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            iBasePresenter.success(code ,string);
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }).start();

    }

    /**
     * 带Token的Get请求
     */
    public  void HttpGetToken(String url, String code ,Activity activity , IBasePresenter iBasePresenter ) {
        SharedPreferences sharedPreferences = activity.getSharedPreferences("app", Context.MODE_PRIVATE);
        new Thread(()->{
            Response response = null ;
            Request request = requestBuilder
                    .url(BASE_URL + url)
                    .header(IContanst.TOKEN , Objects.requireNonNull(sharedPreferences.getString(IContanst.TOKEN, null)))
                    .get()
                    .build();
            try {
                response = mOkHttpClient.newCall(request).execute();
                String string = response.body().string();
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            iBasePresenter.success(code ,string);
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }).start();

    }

    /**
     * 带Token的Post请求
     */
    public  void HttpPostToken(String url, String code , Map<String,Object> paramData ,  Activity activity , IBasePresenter iBasePresenter ) {
        SharedPreferences sharedPreferences = activity.getSharedPreferences("app", Context.MODE_PRIVATE);
        new Thread(()->{
            Response response = null ;
            RequestBody requestBody = RequestBody.create(MediaType.parse("application"), new JSONObject(paramData).toString());
            Request request = requestBuilder
                    .url(BASE_URL + url)
                    .header(IContanst.TOKEN , Objects.requireNonNull(sharedPreferences.getString(IContanst.TOKEN, null)))
                    .post(requestBody)
                    .build();
            try {
                response = mOkHttpClient.newCall(request).execute();
                String string = response.body().string();
                activity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            iBasePresenter.success(code ,string);
                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                });
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }).start();

    }

}
